package com.chinadci.smartcommunitysystemreception.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JWTUtils {

    /**
     * private 随机言
     */
    private static final String SING="!Q@W#E$R%T^Y&U*I(O)P_{+}";

    /**
     * 获取token
     * @param map
     * @return
     */
    public static String GetToken(Map<String,String> map){
        Calendar calendar=Calendar.getInstance();
        calendar.add(Calendar.DATE,1);
        JWTCreator.Builder builder= JWT.create();
        map.forEach((k,v)->{
            builder.withClaim(k,v);
        });
        return builder.withExpiresAt(calendar.getTime()).sign(Algorithm.HMAC256(SING));
    }

    /**
     *  判断令牌是否合法
     * @param token
     * @return  返回令牌中的数据
     */
    public static DecodedJWT Verify(String token){
        return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }
}
